package com.csxh.web.userinfo.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.csxh.web.userinfo.util.WebUtil;

/**
 * Servlet implementation class HtmlServlet
 */
@WebServlet("/fragment.servlet")
public class FragmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FragmentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//获取请求的片断模板文件名称
		String fragment=WebUtil.strParam(request, WebUtil.FRAGMENT, "");
		
		RequestDispatcher rdp=request.getRequestDispatcher("WEB-INF/fragment/"+fragment);
		response.setCharacterEncoding("utf-8");
		if(null!=rdp){
		    //将参数变成上下文中的对象
			Map<String, String[]> map=request.getParameterMap();
			for(Map.Entry<String, String[]>entry:map.entrySet()){
				String key=entry.getKey();
				String[] values=entry.getValue();
				if(values.length>1){
					//传入的是数组
					request.setAttribute(key, values);
				}else{
					//传入的是单值
					request.setAttribute(key, values[0]);
					
				}
			}
			rdp.include(request, response);
			
		}else{
			response.getWriter().append("");
		}
		
	}

}
